package com.taobao.taokeeper.dao.impl;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.taobao.common.toolkit.dbcp.DBConnectionResource;
import com.taobao.common.toolkit.dbcp.DataSourceManager;
import com.taobao.common.toolkit.entity.DateFormat;
import com.taobao.common.toolkit.exception.DaoException;
import com.taobao.common.toolkit.util.DateUtil;
import com.taobao.common.toolkit.util.StringUtil;
import com.taobao.taokeeper.common.constant.SqlTemplate;
import com.taobao.taokeeper.dao.TaoKeeperStatDAO;
import com.taobao.taokeeper.model.TaoKeeperStat;

/**
 * Description: Access DB for taokeeper_stat
 * @author   yinshi.nc
 * @since 2012-01-05
 */
public class TaoKeeperStatDAOImpl implements TaoKeeperStatDAO{
	
	@Override
	public void addTaoKeeperStat( TaoKeeperStat taoKeeperStat ) throws DaoException{
		
		if( null == taoKeeperStat ){
			return;
		}
    	try {
    		String insertSql = StringUtil.replaceSequenced( SqlTemplate.SQL_INSERT_TAOKEEPER_STAT, 
    														taoKeeperStat.getClusterId() + "", 
    														StringUtil.trimToEmpty( taoKeeperStat.getServer() ),
    														StringUtil.defaultIfBlank( taoKeeperStat.getStatDateTime(), DateUtil.getNowTime( DateFormat.DateTime ) ),
    														StringUtil.defaultIfBlank( taoKeeperStat.getStatDate(), DateUtil.getNowTime( DateFormat.Date ) ),
    														taoKeeperStat.getConnections() + "",
    														taoKeeperStat.getWatches() + "",
    														taoKeeperStat.getSendTimes() + "",
    														taoKeeperStat.getReceiveTimes() + "",
    														taoKeeperStat.getNodeCount() + "" );
    		
			DataSourceManager.executeInsert( insertSql );
		} catch ( Exception e ) {
			throw new DaoException( "Error when add taoKeeperStat" + taoKeeperStat + ", Error: " + e.getMessage(), e );
		}
	}

	
	
	@Override
	public List<TaoKeeperStat> queryTaoKeeperStatByClusterIdAndServerAndStatDate( int clusterId, String server, String statDate ) throws DaoException {
		
		if( 0 == clusterId || StringUtil.isBlank( server ) || StringUtil.isBlank( statDate )  ){
			return new ArrayList< TaoKeeperStat >();
		}
		
		List< TaoKeeperStat > taoKeeperStatList = new ArrayList< TaoKeeperStat >();

		ResultSet rs = null;
		DBConnectionResource myResultSet = null;
    	try {
    		myResultSet = DataSourceManager.executeQuery( StringUtil.replaceSequenced( SqlTemplate.SQL_QUERY_TAOKEEPER_STAT_BY_CLUSTERID_SERVER_DATE, clusterId, server, statDate ) );
    		if( null == myResultSet ){
    			return new ArrayList< TaoKeeperStat >();
    		}
    		rs = myResultSet.resultSet;
    		if( null == rs ){
    			return new ArrayList< TaoKeeperStat >();
    		}
			while( rs.next() ){

				server  			    = StringUtil.trimToEmpty( rs.getString( "server" ) );
				String statDateTime     = StringUtil.trimToEmpty( rs.getString( "stat_date_time" ) );
				int connections			= rs.getInt( "connections" );
				int watches             = rs.getInt( "watches" );
				int sendTimes           = rs.getInt( "send_times" );
				int receiveTimes		= rs.getInt( "receive_times" );
				int nodeCount			= rs.getInt( "node_count" );
				taoKeeperStatList.add( new TaoKeeperStat( clusterId, server, statDateTime, statDate, connections, watches, sendTimes, receiveTimes, nodeCount) );
			}
			return taoKeeperStatList;
		} catch ( Exception e ) {
			throw new DaoException( "Error when queryTaoKeeperStatByClusterIdAndServerAndStatDate: clusterId:"+ clusterId +" server:"+ server +"statDate:"+ statDate +", Error: " + e.getMessage(), e );
		}finally{
			if( null != myResultSet ){
				DataSourceManager.closeResultSetAndStatement( rs, myResultSet.statement );
				DataSourceManager.returnBackConnectionToPool( myResultSet.connection );
			}
		}
		
		
	}
	
	
	
	
	
}
